<template>
  <div class='container'>
     <!-- <h1>组题列表</h1> -->
     <el-container>
        <el-header>
           <el-form :inline="true" :model="Query" class="demo-form-inline">
             <el-form-item label="关键字">
               <el-input  v-model="Query.keyword" placeholder="根据编号搜索"></el-input>
             </el-form-item>
             <el-form-item class="el-right">
               <el-button @click="clearKey">清除</el-button>
               <el-button type="primary" @click="onSubmit">搜索</el-button>
             </el-form-item>
           </el-form>
        </el-header>
        <el-main>
          <div class="dataTotal"><i class="el-icon-info"></i> 数据一共{{ counts }}条</div>
          <el-table
          :data="randomsItem"
          style="width: 100% ">
            <el-table-column
              prop="id"
              label="编号"
              width="290">
            </el-table-column>
            <el-table-column
              prop="questionType"
              label="题型"

              align='center'>
            </el-table-column>
            <!-- prop="questionIDs" -->
            <el-table-column
              label="题目编号"
              width="200"
              align='center'
              >
              <template slot-scope="scope">
                <div v-for="item in scope.row.questionIDs" :key="item" style="color:rgb(0,153,255)" @click="Tad(item)"> {{item.substring(0, item.indexOf('?'))}}</div>
             </template>
            </el-table-column>
            <el-table-column
              prop="addTime"
              label="录入时间"
              width="200"
              align='center'>
            </el-table-column>
            <el-table-column
              prop="totalSeconds"
              label="答题时间(s)"

              align='center'>
            </el-table-column>
            <el-table-column
              prop="accuracyRate"
              label="正确率(%)"

              align='center'>
            </el-table-column>
            <el-table-column
              prop="userName"
              label="录入人"

              align='center'>
            </el-table-column>
            <el-table-column
              label="操作"

              align='center'>
               <template slot-scope="scope">
                <el-button  icon="el-icon-delete" circle type="danger" plain size="mini" @click="delData(scope.row)"></el-button>
             </template>

            </el-table-column>
          </el-table>
        </el-main>
        <el-footer>
          <div class="right">
             <el-pagination
              background
              layout="total, prev, pager, next, sizes ,jumper "
              :page-sizes="[5,20,30,50,100]"
              :page-size="Query.pagesize"
              :total="counts"
              @size-change="sizeChange"
              @current-change="currentChange"
              >
             </el-pagination>
          </div>
        </el-footer>
     </el-container>
     <QuestionsPreview
      ref="QuestionsPreview"
       :show-dialog="showDialog"
       :catalogIDList="catalogIDList"
       @update="showDialog = $event">
     </QuestionsPreview>
 </div>
</template>

<script>
import { simple } from '@/api/hmmm/directorys'
import { randoms, removeRandoms } from '@/api/hmmm/questions'
import QuestionsPreview from '@/module-hmmm/components/questions-previewX.vue'
export default {
  data () {
    return {
      Query: {
        page: 1,
        pagesize: 20,
        keyword: ''
      },
      counts: 0,
      randomsItem: [],
      showDialog: false,
      catalogIDList: [],
      random: {
        id: '',
        isNext: ''
      }// 获取基础题目详情需要的参数
    }
  },
  created () {
    this.randoms(this.Query)
    this.simple()
  },
  methods: {
    async simple  () {
      const { data } = await simple()
      this.catalogIDList = data
      // console.log(this.catalogIDList)
    },

    onSubmit () {
      this.randoms(this.Query)
    },
    clearKey () {
      this.Query.keyword = ''
    },
    async randoms (Query) {
      // const ids = {}
      const { data } = await randoms(Query)
      this.counts = data.counts
      this.randomsItem = data.items
      this.Query.page = this.randomsItem.length === 0 ? this.Query.page - 1 : this.Query.page
      for (let i = 0; i < this.randomsItem.length; i++) {
        if (this.randomsItem[i].questionType === '1') {
          this.randomsItem[i].questionType = '单选'
        } else if (this.randomsItem[i].questionType === '2') {
          this.randomsItem[i].questionType = '多选'
        } else if (this.randomsItem[i].questionType === '3') {
          this.randomsItem[i].questionType = '简答'
        }
        const { questionIDs } = data.items[i]
        // console.log(questionIDs)
        const arr = []
        for (let j = 0; j < questionIDs.length; j++) {
          const { number, id } = questionIDs[j]
          arr.push(number + '?' + id)
        }
        this.randomsItem[i].questionIDs = arr
      }
    },
    async Del () {
      const { data } = await randoms(this.Query)
      // console.log(data.items.length)
      this.Query.page = data.items.length === 0 ? this.Query.page - 1 : this.Query.page
      this.randoms(this.Query)
    },
    // async delData (item) {
    //   await removeRandoms(item)
    //   this.Del()
    // },
    delData (item) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await removeRandoms(item)
        this.Del()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    sizeChange (val) {
      this.Query.pagesize = val
      this.randoms(this.Query)
    },
    currentChange (val) {
      this.Query.page = val
      this.randoms(this.Query)
    },
    async Tad (item) {
      item = item.substring(item.indexOf('?') + 1, item.length) - 0
      this.random.id = item
      this.showDialog = true
      // console.log(await detail(this.random))
      this.$refs.QuestionsPreview.detail(this.random)
      // this.$refs.QuestionsPreview.simple()
    }
  },
  components: {
    QuestionsPreview
  }
}
</script>

<style scoped lang='scss'>
 .el-header {
    background-color: #fff;
    color: #333;
    padding: 20px 0 10px 40px;
    position: relative;
    .el-right{
      position: absolute;
      right: 10px;
    }
  }
  .el-main {
    background-color: #FFF;
    color: #333;
    // height: 300px;
    position: relative;
    .dataTotal{
      height: 30px;
      background-color: #E9EEF3;
      line-height: 30px;
      padding-left: 15px;
      margin-bottom: 15px;
      text-align: left;
      color: #929496;
      font-size:12px;
    }
  }
  .el-footer {
     background-color: #FFF;
     color: #333;
     position: relative;
    .right{
       position: absolute;
       right: 0;
    }
  }

  body .el-container {
    margin: 10px;
    padding: 10px;
  }
  </style>
